﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillData();
    }
    void FillData()
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument doc = XDocument.Load(path);
        var student = from st in doc.Element("Students").Elements("Student")
                      select new
                      {
                          ID = st.Element("ID").Value,
                          Name = st.Element("Name").Value,
                          Address = st.Element("Address").Value
                      };

        GridView1.DataSource = student;
        GridView1.DataBind();
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //string path = Server.MapPath("~/FPT-Aptech2.xml");
        //XDocument doc = XDocument.Load(path);
        //var student = doc.Descendants("Students").Single();
        //if (student != null)
        //{
        //    XElement el = new XElement("Student");
        //    el.Add(new XElement("ID", txtID.Text));
        //    el.Add(new XElement("Name", txtName.Text));
        //    el.Add(new XElement("Address", txtAddress.Text));
        //    student.Add(el);
        //    doc.Save(path);
        //    FillData();
        //    setnullTextbox();
        //    Response.Write("<script LANGUAGE='JavaScript' >alert('Add Success!!')</script>");
        //}
        //else
        //{
        //    Response.Write("<script LANGUAGE='JavaScript' >alert('Add not Success!!')</script>");
        //}
        AddData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //if (GridView1.SelectedIndex != -1)
        //{
        //    GridViewRow row = GridView1.SelectedRow;
        //    string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
        //    XDocument document = XDocument.Load(Server.MapPath("~/FPT-Aptech2.xml"));
        //    var deleteQuery = from r in document.Descendants("Student") where r.Element("ID").Value == Id select r;
        //    deleteQuery.First().Remove();
        //    document.Save(Server.MapPath("~/FPT-Aptech2.xml"));
        //    FillData();

        //}
        DeleteData();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (GridView1.SelectedIndex != -1)
        {
            GridViewRow row = GridView1.SelectedRow;

            txtID.Text = HttpUtility.HtmlDecode(row.Cells[1].Text);
            txtName.Text = HttpUtility.HtmlDecode(row.Cells[2].Text);
            txtAddress.Text = HttpUtility.HtmlDecode(row.Cells[3].Text);
        }
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        //if (GridView1.SelectedIndex != -1)
        //{
        //    if (btnupdate.Text == "Update")
        //    {
        //        btnupdate.Text = "Insert";
        //        Disable();
        //        btnupdate.Enabled = true;
        //        txtID.Enabled = false;
        //    }
        //    else if (btnupdate.Text == "Insert")
        //    {
        //        if (txtID.Text.Trim() != null && txtName.Text.Trim() != null && txtAddress.Text.Trim() != null)
        //        {
        //            GridViewRow row = GridView1.SelectedRow;
        //            string Id = HttpUtility.HtmlDecode(row.Cells[1].Text);
        //            XElement xEmp = XElement.Load(Server.MapPath("~/FPT-Aptech2.xml"));
        //            var student = from st in xEmp.Elements("Student") where st.Element("ID").Value == Id select st;
        //            student.First().Element("Name").Value = txtName.Text.Trim();
        //            student.First().Element("Address").Value = txtAddress.Text.Trim();
        //            xEmp.Save(Server.MapPath("~/FPT-Aptech2.xml"));
        //            FillData();
        //            Enable();
        //            btnupdate.Text = "Update";
        //            txtID.Enabled = true;
        //            setnullTextbox();
        //        }
        //        else
        //        {
        //            txtName.Text = "";
        //            txtAddress.Text = "";
        //            txtName.Focus();
        //        }
        //    }
        //}
        UpdateData();
    }
    protected void Enable()
    {
        btnAdd.Enabled = true;
        btnDelete.Enabled = true;
        btnupdate.Enabled = true;
        GridView1.Enabled = true;

    }
    protected void Disable()
    {
        btnAdd.Enabled = false;
        btnDelete.Enabled = false;
        btnupdate.Enabled = false;
        GridView1.Enabled = false;

    }
    protected void setnullTextbox()
    {
        txtID.Text = "";
        txtName.Text = "";
        txtAddress.Text = "";


    }
    /*
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //string path = Server.MapPath("~/FPT-Aptech2.xml");
        //XDocument doc = XDocument.Load(path);
        //if (DropDownList1.SelectedItem.Text == "ID")
        //{
        //    //var SearcdID = from student in xEmp.Elements("Students")
        //    //               where (string)student.Element("ID") == txtID.Text.Trim()
        //    //               select student;
        //    string Id = txtSearch.Text.Trim();
        //    var SearcdID = from student in doc.Element("Students").Elements("Student")
        //                   where student.Element("ID").Value == Id
        //                   select new
        //                   {
        //                       ID = student.Element("ID").Value,
        //                       Name = student.Element("Name").Value,
        //                       Address = student.Element("Address").Value
        //                   };

        //    GridView1.DataSource = SearcdID.ToList();
        //    GridView1.DataBind();

        //}
        //else if (DropDownList1.SelectedItem.Text == "Name")
        //{
        //    string Id = txtSearch.Text.Trim();
        //    var SearcdID = from student in doc.Element("Students").Elements("Student")
        //                   where student.Element("Name").Value == Id
        //                   select new
        //                   {
        //                       ID = student.Element("ID").Value,
        //                       Name = student.Element("Name").Value,
        //                       Address = student.Element("Address").Value
        //                   };

        //    GridView1.DataSource = SearcdID.ToList();
        //    GridView1.DataBind();
        //}
        SearchData();
    }


     */


    // LINQ to XML : Add new Data
    void AddData()
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument xDoc = XDocument.Load(path);

        // Kiem tra xem StudentID da ton tai chua, neu ton tai roi thi ko cho add moi.
        if (xDoc.Element("Students").Elements("Student").Where(s => s.Element("ID").Value.Equals(txtID.Text)).Count() == 1)
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Student ID already exists!!')</script>");
        }
        else
        {
            xDoc.Element("Students").Elements("Student").Last().AddAfterSelf(
                    new XElement("Student",
                        new XElement("ID", txtID.Text),
                        new XElement("Name", txtName.Text),
                        new XElement("Address", txtAddress.Text)
                        ));
            xDoc.Save(path);
            FillData();
        }
    }

    //LINQ to XML : Update Data
    void UpdateData()
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument xDoc = XDocument.Load(path);

        //Kiem tra StudentID co ton tai ko, neu ton tai thi moi cho update
        if (xDoc.Element("Students").Elements("Student").Where(s => s.Element("ID").Value.Equals(txtID.Text)).Count() == 1)
        {
            XElement xEle = xDoc.Element("Students").Elements("Student").Where(s => s.Element("ID").Value.Equals(txtID.Text)).Single();
            xEle.Element("Name").Value = txtName.Text;
            xEle.Element("Address").Value = txtAddress.Text;
            xDoc.Save(path);
            FillData();
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Student ID not exists!!')</script>");
        }
    }

    //LINQ to XML : Delete Data
    void DeleteData()
    {
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument xDoc = XDocument.Load(path);

        //Kiem tra StudentID co ton tai ko, neu ton tai thi moi cho Delete
        if (xDoc.Element("Students").Elements("Student").Where(s => s.Element("ID").Value.Equals(txtID.Text)).Count() == 1)
        {
            xDoc.Element("Students").Elements("Student").Where(s => s.Element("ID").Value.Equals(txtID.Text)).Remove();
            xDoc.Save(path);
            FillData();
        }
        else
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('Student ID not exists!!')</script>");
        }
    }

    //LINQ to XML: Search Data
    void SearchData()
    {
       
        string path = Server.MapPath("~/FPT-Aptech2.xml");
        XDocument xDoc = XDocument.Load(path);
        var student = from s in xDoc.Element("Students").Elements("Student")
                      where s.Element("ID").Value.StartsWith(txtSearch.Text)
                      select new
                      {
                          ID = s.Element("ID").Value,
                          Name = s.Element("Name").Value,
                          Address = s.Element("Address").Value
                      };

        GridView1.DataSource = student;
        GridView1.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchData();
    }
    
}